package com.qiguliuxing.dts.admin.web;

import java.util.List;

import javax.validation.constraints.NotNull;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.qiguliuxing.dts.admin.annotation.RequiresPermissionsDesc;
import com.qiguliuxing.dts.admin.service.AdminPublishService;
import com.qiguliuxing.dts.admin.util.AuthSupport;
import com.qiguliuxing.dts.core.validator.Order;
import com.qiguliuxing.dts.core.validator.Sort;

@RestController
@RequestMapping("/admin/publish")
@Validated
public class AdminPublishController {
	private static final Logger logger = LoggerFactory.getLogger(AdminPublishController.class);

	@Autowired
	private AdminPublishService adminPublishService;

	/**
	 * 查询发布单
	 */
	@RequiresPermissions("admin:publish:list")
	@RequiresPermissionsDesc(menu = { "商场管理", "发布单管理" }, button = "查询")
	@GetMapping("/list")
	public Object list(Integer userId, String publishSn, 
			@RequestParam(defaultValue = "1") Integer page, 
			@RequestParam(defaultValue = "10") Integer limit,
			@Sort @RequestParam(defaultValue = "add_time") String sort,
			@Order @RequestParam(defaultValue = "desc") String order) {
		logger.info("【请求开始】操作人:[" + AuthSupport.userName()+ "] 商场管理->发布单管理->查询,请求参数:userId:{},publishSn:{},page:{}", userId, publishSn, page);

		return adminPublishService.list(userId, publishSn, page, limit, sort, order);
	}

	/**
	 * 发布单详情
	 */
	@RequiresPermissions("admin:publish:read")
	@RequiresPermissionsDesc(menu = { "商场管理", "发布单管理" }, button = "详情")
	@GetMapping("/detail")
	public Object detail(@NotNull Integer id) {
		logger.info("【请求开始】操作人:[" + AuthSupport.userName()+ "] 商场管理->发布单管理->详情,请求参数:id:{}", id);

		return adminPublishService.detail(id);
	}

	/**
	 * 处理发布单
	 */
	@RequiresPermissions("admin:publish:handle")
	@RequiresPermissionsDesc(menu = { "商场管理", "发布单管理" }, button = "处理")
	@PostMapping("/handle")
	public Object handle(@RequestBody String body) {
		logger.info("【请求开始】操作人:[" + AuthSupport.userName()+ "] 商场管理->发布单管理->处理,请求参数,body:{}", body);

		return adminPublishService.handle(body);
	}
}